package com.study.meng.bigevent.controller;

import com.study.meng.bigevent.common.utils.ThreadLocalUtil;
import com.study.meng.bigevent.entity.Article;
import com.study.meng.bigevent.entity.PageBean;
import com.study.meng.bigevent.entity.Result;
import com.study.meng.bigevent.service.ArticleService;
import com.study.meng.bigevent.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private UserService userService;

    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        Integer userId = Integer.valueOf(ThreadLocalUtil.getCurrentUser().toString());
        article.setCreateUser(userId);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        boolean flag = articleService.add(article);
        return flag ? Result.success() : Result.error(401, "添加失败！");
    }

    @GetMapping("/list")
    public Result<PageBean<Article>> list(Integer page,
                                          Integer pageSize,
                                          @RequestParam(required = false) Integer categoryId,
                                          @RequestParam(required = false) String state) {
        Integer createUser = Integer.valueOf(ThreadLocalUtil.getCurrentUser().toString());
        return Result.success(articleService.list(page, pageSize, categoryId, state, createUser));
    }

}
